package com.firebird.app.fekpsl.ui.activity.me

import android.text.InputFilter
import android.text.InputFilter.LengthFilter
import android.text.InputType
import android.view.View
import com.firebird.app.fekpsl.R
import com.firebird.app.fekpsl.base.BaseMvpActivity
import com.firebird.app.fekpsl.constant.Constant
import com.firebird.app.fekpsl.mvp.me.BindPhoneContract
import com.firebird.app.fekpsl.mvp.me.BindPhonePresenter
import com.firebird.app.fekpsl.utils.Preference
import kotlinx.android.synthetic.main.me_bind_phone.*
import kotlinx.android.synthetic.main.toolbar.*

class BindPhoneActivity : BaseMvpActivity<BindPhoneContract.View, BindPhoneContract.Presenter>(), BindPhoneContract.View {
    private var pf_bind_phone:String by Preference(Constant.PHONE_KEY, "")
    private var pf_uuid:String by Preference(Constant.UUID_KEY, "")

    override fun createPresenter(): BindPhoneContract.Presenter = BindPhonePresenter()

    private val mDialog by lazy {
        // DialogUtil.getWaitDialog(this, getString(R.string.save_ing))
    }

    override fun showLoading() {
        // mDialog.show()
    }

    override fun hideLoading() {
        // mDialog.dismiss()
    }

    override fun bindPhoneSuccess(success: Boolean) {
        if(success){
            pf_bind_phone=et_me_bind_phone.text.toString()
            finish()
        }else{
            // showToast(getString(R.string.me_save_fail))
        }
    }
    /*
        override fun updateNiceNameFail() {
            showToast(getString(R.string.me_save_fail))
        }*/
    override fun attachLayoutRes(): Int = R.layout.me_bind_phone

    override fun initData() {
    }

    override fun initView() {
        super.initView()

        toolbar.run {
            title = resources.getString(R.string.me_bind_phone)
            setSupportActionBar(this)
            supportActionBar?.setDisplayHomeAsUpEnabled(true)
        }
        et_me_bind_phone.setInputType(InputType.TYPE_CLASS_PHONE)
        et_me_bind_phone.setFilters(arrayOf<InputFilter>(LengthFilter(11))) //限制输入长度为11
        //et_me_bind_phone.setInputType(InputType.TYPE_CLASS_NUMBER) //输入类型限制为数字
        et_me_bind_phone.setText(pf_bind_phone)
        btn_save.setOnClickListener(onClickListener)
    }

    override fun start() {
    }

    /**
     * OnClickListener
     */
    private val onClickListener = View.OnClickListener { view ->
        when (view.id) {
            R.id.btn_save -> {
                bindPhone()
            }
        }
    }
    private fun bindPhone() {
        if (validate()) {
            mPresenter?.bindPhone(pf_uuid, et_me_bind_phone.text.toString()
            )
        }
    }
    /**
     * check data
     */
    private fun validate(): Boolean {
        var valid = true
        val bindPhone: String = et_me_bind_phone.text.toString()

        if (bindPhone.isEmpty()) {
            et_me_bind_phone.error = getString(R.string.me_bind_phone_not_empty)
            valid = false
        }
        if (bindPhone.length>11){
            et_me_bind_phone.error = getString(R.string.me_bind_phone_len_error)
            valid = false
        }
        return valid
    }

}